package com.bci.pwtz.controller.app;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.DeviceDictConstants;
import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ObjResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.DydlfjxReq;
import com.bci.pwtz.common.remote.req.DydlfjxIdReq;
import com.bci.pwtz.common.remote.req.DydlfjxListReq;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Dydlfjx;
import com.bci.pwtz.service.DictionaryAuditService;
import com.bci.pwtz.service.DydlfjxService;
@Controller
@RequestMapping("/api/dydlfjx")
public class DydlfjxAppController extends AbstractController{
@Autowired
DydlfjxService service;//注解注入服务
@Autowired
DictionaryAuditService dictionaryAuditService;
/**
 * app端请求获取设备列表方法
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:15:36<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public ListResponse<Dydlfjx> getDydlfjxList(HttpServletRequest request){
	DydlfjxListReq req=null;
	PaginationList<Dydlfjx> pagination=null;
	ListResponse<Dydlfjx> resp=new ListResponse<Dydlfjx>();
	try{
		req=getJsonToObj(request,DydlfjxListReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空");
			return resp;
		}
		if(req.getCompanyId()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			resp.setTimestamp(req.getTimestamp());
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
			resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
        	resp.setTimestamp(req.getTimestamp());
            return resp;
        }
		String beginTime=req.getBeginTime();
		Long companyId=req.getCompanyId();
		String devicename=req.getDeviceName();
		pagination=service.selectDydlfjxs(devicename,companyId,PwtzConstants.PORTAL_TYPE_APP,req.getMedLineCodes(),null,beginTime, null, req.getPageSize(),req.getPage(),null);
		if(pagination !=null){
			resp.setReturnCode(ReturnCode.SUCCESS);
			resp.setReturnMsg("查询低压电缆分接箱列表成功");
			resp.setObjs(pagination);
			resp.setTimestamp(req.getTimestamp());
		}else{
			resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
			resp.setReturnMsg("查询低压电缆分接箱列表调用接口返回失败");
			resp.setTimestamp(req.getTimestamp());
		}
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(req.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		req.setTimestamp(resp.getTimestamp());
	}
	return resp;
}
/**
 * app端请求添加设备方法
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:16:40<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public ObjResponse<Dydlfjx> addDydlfjx(HttpServletRequest request){
	DydlfjxReq req=null;
	ObjResponse<Dydlfjx> resp=new ObjResponse<Dydlfjx>();
	try{
		req=getJsonToObj(request,DydlfjxReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getObj()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			resp.setTimestamp(req.getTimestamp());
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
			resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
        	resp.setTimestamp(req.getTimestamp());
            return resp;
        }
		Dydlfjx rtObj=req.getObj();
		int result=0;
		if(service.load(rtObj.getDeviceid())!=null){
		    result=service.update(rtObj);
		}else{
		    rtObj=service.add(rtObj);
		}
		if(rtObj !=null||result>0){
		    rtObj=service.load(rtObj.getDeviceid());
		    dictionaryAuditService.add(DeviceDictConstants.DYDLFJX_MANUFACTURE_FACTORY_DICT_ID, req.getObj().getProductionfactory());
		    dictionaryAuditService.add(DeviceDictConstants.DYDLFJX_MODEL_DICT_ID, req.getObj().getModel());
			resp.setReturnCode(ReturnCode.SUCCESS);
			resp.setReturnMsg("添加低压电缆分接箱成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
			resp.setObj(rtObj);
			resp.setTimestamp(req.getTimestamp());
			setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压电缆分接箱,AbstractController.add,"添加低压电缆分接箱["+rtObj.getDevicename()+"]成功",req.getUserId());
		}else{
			resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
			resp.setReturnMsg("添加低压电缆分接箱调用接口返回失败");
			resp.setTimestamp(req.getTimestamp());
		}
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(req.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		req.setTimestamp(resp.getTimestamp());
	}
	return resp;
}
/**
 * app端请求删除设备id对应的设备
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:17:50<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ObjResponse<Dydlfjx> delete(HttpServletRequest request){
	DydlfjxIdReq req=null;
	ObjResponse<Dydlfjx> resp=new ObjResponse<Dydlfjx>();
	try{
		req=getJsonToObj(request,DydlfjxIdReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getDeviceId()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			resp.setTimestamp(req.getTimestamp());
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
			resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
        	resp.setTimestamp(req.getTimestamp());
            return resp;
        }
		String deviceid=req.getDeviceId();
		Dydlfjx rtObj=service.load(deviceid);
		boolean flag=service.deleteDevice(deviceid);
		if(flag){
		    int result=service.delete(deviceid);
	        if(result>0&&rtObj!=null){
	            rtObj=service.load(deviceid);
	            resp.setReturnCode(ReturnCode.SUCCESS);
	            resp.setReturnMsg("删除低压电缆分接箱成功");
	            rtObj.setLastoperatorid(req.getUserId());
	            service.update(rtObj);
	            resp.setTimestamp(req.getTimestamp());
	            resp.setObj(rtObj);
	            setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压电缆分接箱,AbstractController.delete,"删除低压电缆分电箱["+rtObj.getDevicename()+"]成功",req.getUserId());
	        }else{
	            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
	            resp.setReturnMsg("删除低压电缆分接箱调用接口返回失败");
	            resp.setTimestamp(req.getTimestamp());
	        }
		}else{
		    resp.setReturnCode(ReturnCode.ERROR_DELETE_REF_DEVICE);
            resp.setReturnMsg("删除相关低压电缆分接箱返回失败");
            resp.setTimestamp(req.getTimestamp());
		}
	}catch(CanNotDeleteException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("存在依赖关系，不能删除");
		resp.setTimestamp(req.getTimestamp());
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(req.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		req.setTimestamp(resp.getTimestamp());
	}
	return resp;
}
/**
 * app端请求修改对应的设备
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:18:52<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/update",method=RequestMethod.POST)
@ResponseBody
public ObjResponse<Dydlfjx> update(HttpServletRequest request){
	DydlfjxReq req=null;
	ObjResponse<Dydlfjx> resp=new ObjResponse<Dydlfjx>();
	try{
		req=getJsonToObj(request,DydlfjxReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getObj()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			resp.setTimestamp(req.getTimestamp());
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
			resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        Dydlfjx rtObj=req.getObj();
		int result=service.update(rtObj);
		if(result>0){
		    rtObj=service.load(rtObj.getDeviceid());
		    dictionaryAuditService.add(DeviceDictConstants.DYDLFJX_MANUFACTURE_FACTORY_DICT_ID, req.getObj().getProductionfactory());
            dictionaryAuditService.add(DeviceDictConstants.DYDLFJX_MODEL_DICT_ID, req.getObj().getModel());
			resp.setReturnCode(ReturnCode.SUCCESS);
			resp.setReturnMsg("修改低压电缆分接箱成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
			resp.setTimestamp(req.getTimestamp());
			resp.setObj(rtObj);
			setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压电缆分接箱,AbstractController.update,"低压电缆分接箱修改["+rtObj.getDevicename()+"]成功",req.getUserId());
		}else{
			resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
			resp.setReturnMsg("修改低压电缆分接箱调用接口返回失败");
			resp.setTimestamp(req.getTimestamp());
		}
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(req.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		req.setTimestamp(resp.getTimestamp());
	}
	return resp;
}
/**
 * app端请求查询设备id对应的设备信息
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:19:40<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/load",method=RequestMethod.POST)
@ResponseBody
public ObjResponse<Dydlfjx> getObj(HttpServletRequest request){
	DydlfjxIdReq req=null;
	ObjResponse<Dydlfjx> resp=new ObjResponse<Dydlfjx>();
	try{
		req=getJsonToObj(request,DydlfjxIdReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getDeviceId()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			resp.setTimestamp(req.getTimestamp());
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
			resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
        	resp.setTimestamp(req.getTimestamp());
            return resp;
        }
		Dydlfjx rtObj=service.load(req.getDeviceId());
		if(rtObj !=null){
			resp.setReturnCode(ReturnCode.SUCCESS);
			resp.setReturnMsg("查询低压电缆分接箱成功");
			resp.setObj(rtObj);
			resp.setTimestamp(req.getTimestamp());
		}else{
			resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
			resp.setReturnMsg("查询低压电缆分接箱调用接口返回失败");
			resp.setTimestamp(req.getTimestamp());
		}
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(req.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		req.setTimestamp(resp.getTimestamp());
	}
	return resp;
}
}
